package cn.lyw.discount.service.service.impl;

import cn.common.beancopy.util.BeanCopierUtil;
import cn.common.init.InitDataConstant;
import cn.common.redis.RedisServie;
import cn.common.restresult.util.RestResult;
import cn.common.restresult.util.ResultEnum;
import cn.hutool.core.lang.generator.ObjectIdGenerator;
import cn.lyw.discount.mapper.mapper.AdminDiscountMapper;
import cn.lyw.discount.mapper.pojo.SmallMiAdminDiscount;
import cn.lyw.discount.service.bo.AddAdminDiscountBO;
import cn.lyw.discount.service.bo.DelAdminDiscountBO;
import cn.lyw.discount.service.bo.EditAdminDiscountBO;
import cn.lyw.discount.service.bo.GetAdminDiscountByStateBO;
import cn.lyw.discount.service.domain.GetAdminDiscountAllDO;
import cn.lyw.discount.service.domain.GetAdminDiscountByIdDO;
import cn.lyw.discount.service.domain.GetAdminDiscountByStateDO;
import cn.lyw.discount.service.service.AdminDiscountService;
import cn.lyw.discount.utils.DiscountPond;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author lyw
 * @Description 后台优惠券逻辑层
 * @Date 16:03 2021/7/18
 * @Param
 * @return
 */

@Service
public class AdminDiscountServiceImpl implements AdminDiscountService {

    @Autowired
    private AdminDiscountMapper adminDiscountMapper;

    @Autowired
    private DiscountPond discountPond;

    @Autowired
    private RedisServie redisServie;

    /**
     * @return
     * @Author lyw
     * @Description 查询全部优惠券
     * @Date 9:04 2021/7/20
     * @Param
     */
    @Override
    public List<GetAdminDiscountAllDO> getAdminDiscountAll() {
        //测试
        discountPond.UserGetDiscount();

        //通过通用mapper查询全部
        List<SmallMiAdminDiscount> smallMiAdminDiscounts = adminDiscountMapper.selectAll();
        //转返回值类型
        List<GetAdminDiscountAllDO> getAdminDiscountAllDOS = BeanCopierUtil.copyList(smallMiAdminDiscounts, GetAdminDiscountAllDO.class);
        return getAdminDiscountAllDOS;
    }


    /**
     * @return
     * @Author lyw
     * @Description 根据状态查询优惠券
     * @Date 9:08 2021/7/20
     * @Param
     */
    @Override
    public List<GetAdminDiscountByStateDO> getAdminDiscountByState(GetAdminDiscountByStateBO bo) {
        //将参数类型转换
        SmallMiAdminDiscount smallMiAdminDiscount = BeanCopierUtil.copyObj(bo, SmallMiAdminDiscount.class);
        //调用通用mapper查询优惠券
        List<SmallMiAdminDiscount> select = adminDiscountMapper.select(smallMiAdminDiscount);
        //转换返回值类型
        List<GetAdminDiscountByStateDO> getAdminDiscountByStateDOS = BeanCopierUtil.copyList(select, GetAdminDiscountByStateDO.class);
        return getAdminDiscountByStateDOS;
    }


    /**
     * @return
     * @Author lyw
     * @Description 修改优惠券
     * @Date 9:12 2021/7/20
     * @Param
     */
    @Override
    public RestResult editAdminDiscount(EditAdminDiscountBO bo) {
        //转换参数
        SmallMiAdminDiscount smallMiAdminDiscount = BeanCopierUtil.copyObj(bo, SmallMiAdminDiscount.class);

        //校验数据
//        if (smallMiAdminDiscount.getDiscountCount()<0){
//            return new RestResult(ResultEnum.EDIT_ERROR);
//        }

        //修改时间，版本
        smallMiAdminDiscount.setModifyDate(new Date());

        if (smallMiAdminDiscount.getVersion() != null) {
            String version = smallMiAdminDiscount.getVersion();
            int oldVersion = Integer.parseInt(version);
            Integer newVersion = oldVersion + 1;
            smallMiAdminDiscount.setVersion(newVersion.toString());
        }


        //调用通用mapper
        int i = adminDiscountMapper.updateByPrimaryKeySelective(smallMiAdminDiscount);
        //判断返回值
        if (i == 1) {
            //如果修改成功就修改redis

            return new RestResult(ResultEnum.EDIT_SUCCESS);
        }

        return new RestResult(ResultEnum.EDIT_ERROR);
    }


    /**
     * @return
     * @Author lyw
     * @Description 添加未开始优惠券
     * @Date 10:16 2021/7/20
     * @Param
     */
    @Override
    public RestResult addAdminDiscount(AddAdminDiscountBO bo) {
        //转换参数
        SmallMiAdminDiscount smallMiAdminDiscount = BeanCopierUtil.copyObj(bo, SmallMiAdminDiscount.class);

        //随机生成优惠券id
        ObjectIdGenerator objectIdGenerator = new ObjectIdGenerator();
        String next = objectIdGenerator.next();
        smallMiAdminDiscount.setDiscountId(next);

        //校验数量
        if (smallMiAdminDiscount.getDiscountCount() < 0) {
            return new RestResult(ResultEnum.ADD_ERROR);
        }

        //创建时间，修改时间，版本号
        smallMiAdminDiscount.setDiscountState("notStart");
        smallMiAdminDiscount.setCreationDate(new Date());
        smallMiAdminDiscount.setModifyDate(new Date());
        smallMiAdminDiscount.setVersion("1");

        //获取时间
        long time1 = smallMiAdminDiscount.getDiscountStart().getTime();
        //当前时间
        long time2 = (new Date()).getTime();
        long l = (time1 - time2) / 1000;

        //添加到redis中
        redisServie.putHash(InitDataConstant.DISCOUNT_NOT_START + smallMiAdminDiscount.getDiscountId(), smallMiAdminDiscount.getDiscountId(), smallMiAdminDiscount, l);

        //调用通用mapper
        int i = adminDiscountMapper.insertSelective(smallMiAdminDiscount);

        //判断返回值
        if (i == 0) {
            return new RestResult(ResultEnum.ADD_ERROR);
        }
        return new RestResult(ResultEnum.ADD_SUCCESS);

    }


    /**
     * @return
     * @Author lyw
     * @Description 删除优惠券
     * @Date 10:36 2021/7/20
     * @Param
     */
    @Override
    public RestResult delAdminDiscount(DelAdminDiscountBO bo) {
        //转换参数
        SmallMiAdminDiscount smallMiAdminDiscount = BeanCopierUtil.copyObj(bo, SmallMiAdminDiscount.class);

        //调用通用mapper
        int i = adminDiscountMapper.deleteByPrimaryKey(smallMiAdminDiscount.getDiscountId());

        if (i == 0) {
            return new RestResult(ResultEnum.DEL_ERROR);
        }
        return new RestResult(ResultEnum.DEL_SUCCESS);
    }


    /**
     * @return
     * @Author lyw
     * @Description 根据id查询优惠券
     * @Date 16:57 2021/7/20
     * @Param
     */
    @Override
    public GetAdminDiscountByIdDO getAdminDiscountById(String discountId) {
        //调用通用mapper
        SmallMiAdminDiscount smallMiAdminDiscount = adminDiscountMapper.selectByPrimaryKey(discountId);
        //转换返回值
        GetAdminDiscountByIdDO getAdminDiscountByIdDO = BeanCopierUtil.copyObj(smallMiAdminDiscount, GetAdminDiscountByIdDO.class);
        return getAdminDiscountByIdDO;
    }

}
